package com.bigfish.gbs.service.impl;

import com.bigfish.gbs.dao.GoodsMapper;
import com.bigfish.gbs.dao.GoodsTempMapper;
import com.bigfish.gbs.dao.ImageMapper;
import com.bigfish.gbs.dto.GoodsVO;
import com.bigfish.gbs.entity.Goods;
import com.bigfish.gbs.entity.GoodsTemp;
import com.bigfish.gbs.entity.Image;
import com.bigfish.gbs.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Joycessh
 * @Description
 * @Date 12/20/18 9:44 AM
 */
@Service("GoodsService")
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsTempMapper goodsTempMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Override
    public List<GoodsTemp> getGoods() {
        return goodsTempMapper.selectGoods();
    }

    @Override
    public List<GoodsVO> getGoodsList() {
        return goodsMapper.selectGoodsList();
    }

    @Override
    public boolean updateGoods(GoodsVO goodsVO) {
        Goods goods = goodsVO.getGoods();
        int num1 = goodsMapper.updateByPrimaryKeySelective(goods);

        Image image = new Image();
        image.setGoodsId(goodsVO.getId());
        image.setImgPath(goodsVO.getImgPath());
        int num2 = imageMapper.updateByGoodsId(image);

        return ((num1 == 1)&&(num2 == 1)) ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public boolean addGoods(GoodsVO goodsVO) {
        Goods goods = goodsVO.getGoods();
        Date date = new Date();
        goods.setStartTime(date);
        int num1 = goodsMapper.insertReturnId(goods);

        Image image =new Image();
        image.setImgPath(goodsVO.getImgPath());
        image.setGoodsId(goods.getId());
        int num2 = imageMapper.insertSelective(image);

        return ((num1 == 1)&&(num2 == 1)) ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public boolean removeGoods(Integer goodsId) {
        int num = goodsMapper.deleteByPrimaryKey(goodsId);
        return num == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public List<GoodsVO> getGoodsByStatus(Byte status) {
        return goodsMapper.selectByStatus(status);
    }

    @Override
    public List<GoodsVO> getGoodsByName(String name) {

        if (name.equals(null) & name.equals("")) {
            return goodsMapper.selectGoodsList();
        }else {
            String likeName = '%' + name + '%';  // 为like模糊查询拼接sql通配符 '%'
            return goodsMapper.selectByName(likeName);
        }
    }
}
